﻿using System;
using Microsoft.Xna.Framework;

namespace D2DEngine
{
    public class D2DFrameAnimation : ICloneable
    {
        Rectangle[] frames;
        int currentFrame = 0;

        float frameLength = .5f;
        float timer = 0;

        /// <summary>
        /// The number of frame per second
        /// </summary>
        public int FramesPerSecond
        {
            get
            {
                return (int)(1f / frameLength);
            }
            set
            {
                frameLength = (float)Math.Max(1f / (float)value, .01f);
            }
        }

        /// <summary>
        /// The bounding rectangle of the
        /// current frame
        /// </summary>
        public Rectangle CurrentRect
        {
            get { return frames[currentFrame]; }
        }

        /// <summary>
        /// The current frame in the animation
        /// </summary>
        public int CurrentFrame
        {
            get { return currentFrame; }
            set
            {
                currentFrame = (int)MathHelper.Clamp(value, 0, frames.Length - 1);
            }
        }

        public D2DFrameAnimation(
            int numberOfFrames,
            int xOffset,
            int yOffset,
            int frameWidth,
            int frameHeight
            )
        {
            frames = new Rectangle[numberOfFrames];

            for (int i = 0; i < numberOfFrames; i++)
            {
                Rectangle rect = new Rectangle();
                rect.Width = frameWidth;
                rect.Height = frameHeight;
                rect.X = xOffset + (i * frameWidth);
                rect.Y = yOffset;

                frames[i] = rect;
            }
        }

        public D2DFrameAnimation(
            int numberOfFrames,
            int xOffset,
            int yOffset,
            int frameWidth,
            int frameHeight,
            float frameLength
            )
        {
            frames = new Rectangle[numberOfFrames];

            for (int i = 0; i < numberOfFrames; i++)
            {
                Rectangle rect = new Rectangle();
                rect.Width = frameWidth;
                rect.Height = frameHeight;
                rect.X = xOffset + (i * frameWidth);
                rect.Y = yOffset;

                this.frames[i] = rect;
            }

            this.frameLength = frameLength;
        }

        private D2DFrameAnimation()
        {
        }

        /// <summary>
        /// Updates the frame animation
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (timer >= frameLength)
            {
                timer = 0f;

                currentFrame = (currentFrame + 1) % frames.Length;
            }
        }

        /// <summary>
        /// Clones the animation frame
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            D2DFrameAnimation frame = new D2DFrameAnimation();

            frame.frameLength = frameLength;
            frame.frames = frames;

            return frame;
        }
    }
}
